package com.swp.oa.sys.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.swp.oa.sys.dto.domian.PermissionGroupDto;
import com.swp.oa.sys.dto.domian.PermissionItemDto;
import com.swp.oa.sys.dto.page.EasyuiDataGridJson;
import com.swp.oa.sys.dto.page.JsonMessage;
import com.swp.oa.sys.pojo.Auth;
import com.swp.oa.sys.service.IAuthService;
import com.swp.oa.sys.util.Pager;
@Controller
@RequestMapping(value="/permission")
public class PermissionController {

	@Autowired
	private IAuthService authService;

	// 返回权限管理首页
	@RequestMapping("/Index")
	public String permissionIndex() {
		
		return "/Permission/Index";
	}

	@RequestMapping(value = "/GetPermissionGroups", method = RequestMethod.POST)
	@ResponseBody
	public EasyuiDataGridJson getPermissionGroups(int page, int rows) {
		EasyuiDataGridJson json = new EasyuiDataGridJson();
		Pager pager = authService.getAllPermissionGroupList(page, rows);
		if(pager != null){
			json.setRows(pager.getRows());
			json.setTotal(pager.getTotalCount());			
		}
		return json;
	}	
	@RequestMapping("/getPermissionItems")
	@ResponseBody
	public List<PermissionItemDto> getPermissionItems(String permissionGroupId)
	{
		List<PermissionItemDto> permissionItems = authService.getPermissionItemsByPermissionGroupId(permissionGroupId);
		return permissionItems;
	}
	/**
	 * 是否创建权限组
	 * @param model
	 * @param systemId
	 * @return
	 */
	@RequestMapping("/toCreatePermissionGroup")
	public String toCreateSystem()
	{
		return "/Permission/CreatePermissionGroup";
	}
	/**
	 * 创建权限组
	 * @param request
	 * @param systemId
	 * @return
	 */
	@RequestMapping(value="/CreatePermissionGroup")
	@ResponseBody
	public JsonMessage CreatePermissionGroup(WebRequest request,PermissionGroupDto permissionGroup)
	{
		JsonMessage jsonMsg = new JsonMessage();
		try {
			authService.savePermissionGroup(permissionGroup);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("创建权限组出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("创建权限组成功！");
	    return jsonMsg;
	}
	/**
	 * 是否编辑权限组
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEditPermissionGroup")
	public String toEditPermissionGroup(Model model,String permissionGroupId)
	{
		model.addAttribute("permissionGroupId", permissionGroupId);
		return "/Permission/EditPermissionGroup";
	}
	
	
	@RequestMapping("/editPermissionForm")
	@ResponseBody
	public PermissionGroupDto editPermissionForm(String permissionGroupId)
	{
		PermissionGroupDto permissionGroupModel= new PermissionGroupDto();
		Auth auth = authService.getAuthByPrimaryKey(permissionGroupId);
		permissionGroupModel.setId(auth.getAuthid());
		permissionGroupModel.setName(auth.getAuthname());
		permissionGroupModel.setDescription(auth.getDescription());
		return permissionGroupModel;
	}
	@RequestMapping(value="/EditPermissionGroup")
	@ResponseBody
	public JsonMessage EditPermissionGroup(WebRequest request,PermissionGroupDto permissionGroup)
	{
		JsonMessage jsonMsg = new JsonMessage();
		try {
			authService.update(permissionGroup);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("编辑权限组出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("编辑权限组成功！");
	    return jsonMsg;
	}
	/**
	 * 删除权限组
	 * @param permissionGroupIds
	 * @return
	 */
	@RequestMapping("/DeletePermissionGroup")
	@ResponseBody
	public JsonMessage DeletePermissionGroup(String permissionGroupIds)
	{
		String[] permissionGroupIdArray = permissionGroupIds.split(",");
		JsonMessage jsonMsg = new JsonMessage();
		try {
			authService.delete(permissionGroupIdArray);	
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("删除权限组失败!");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("删除权限组成功!");
		return jsonMsg;
	}
	@RequestMapping("/toCreatePermissionItem")
	public String toCreatePermissionItem(String permissionGroupId,Model model)
	{
		Auth auth = authService.getAuthByPrimaryKey(permissionGroupId);
		model.addAttribute("permissionGroupId", permissionGroupId);
		model.addAttribute("permissionGroupName", auth.getAuthname());
		return "/Permission/CreatePermissionItem";
	}
	@RequestMapping(value="/createPermissionItem")
	@ResponseBody
	public JsonMessage createPermissionItem(WebRequest request,PermissionItemDto permissionItem)
	{
		JsonMessage jsonMsg = new JsonMessage();
		String flag = authService.existPermissionItem(permissionItem);
		if(flag != null){
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg(flag);
			jsonMsg.setObj(flag);
			return jsonMsg;
		}
		try {
			authService.savePermissionItem(permissionItem);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("创建权限项出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("创建权限项成功！");
	    return jsonMsg;
	}
	@RequestMapping("/toEditPermissionItem")
	public String toEditPermissionItem(String permissionItemId,Model model)
	{
		model.addAttribute("id", permissionItemId);
		return "/Permission/EditPermissionItem";
	}
	@RequestMapping("/editPermissionItemForm")
	@ResponseBody
	public PermissionItemDto editPermissionItemForm(String id)
	{
		Auth auth = authService.getAuthByPrimaryKey(id);
		PermissionItemDto permissionItemModel = new PermissionItemDto();
		permissionItemModel.setId(auth.getAuthid());
		permissionItemModel.setDisplayName(auth.getAuthname());
		permissionItemModel.setPermissionGroupId(auth.getParentauthid());
		permissionItemModel.setCode(auth.getCode());
		Auth group = authService.getAuthByPrimaryKey(auth.getParentauthid());
		permissionItemModel.setPermissionGroupName(group.getAuthname());
		return permissionItemModel;
	}
	@RequestMapping(value="/editPermissionItem")
	@ResponseBody
	public JsonMessage editPermissionItem(WebRequest request,PermissionItemDto permissionItem)
	{
		JsonMessage jsonMsg = new JsonMessage();
		String flag = authService.existEditPermissionItem(permissionItem);
		if(flag != null){
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg(flag);
			jsonMsg.setObj(flag);
			return jsonMsg;
		}
		try {
			authService.updatePermissionItem(permissionItem);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("编辑权限项出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("编辑权限项成功！");
	    return jsonMsg;
	}
	@RequestMapping("/deletePermissionItem")
	@ResponseBody
	public JsonMessage deletePermissionItem(String permissionItemId)
	{
		JsonMessage jsonMsg = new JsonMessage();
		try {
			authService.deleteByPrimaryKey(permissionItemId);	
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("删除权限项失败!");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("删除权限项成功!");
		return jsonMsg;
	}
}
